<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="openSelectUser"
          v-hasPermi="['system:role:add']"
        >新增任免</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['system:role:remove']"
        >批量取消任免</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="职务" align="center" prop="postIds">
        <template slot-scope="scope">
          <dict-tag :options="postOptions2" :value="scope.row.postIds"/>
        </template>
      </el-table-column> -->
      <el-table-column label="姓名" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="getDict('sys_user_sex')" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" key="phonenumber" prop="phonenumber"/>
      <el-table-column label="政治面貌" align="center" prop="politicalOutlook">
        <template #default="scope">
          <dict-tag :hideTag="true" :options="getDict('political_outlook')" :value="scope.row.politicalOutlook"/>
        </template>
      </el-table-column>
      <el-table-column label="电子邮件" align="center" key="email" prop="email"/>
      <el-table-column label="最高学历" align="center" prop="highestEducational">
        <template #default="scope">
          <dict-tag :hideTag="true" :options="getDict('highest_educational')" :value="scope.row.highestEducational"/>
        </template>
      </el-table-column>
      <el-table-column label="最高学历专业" align="center" prop="highestMajor">
        <template #default="scope">
          <dict-tag :hideTag="true" :options="getDict('highest_major')" :value="scope.row.highestMajor"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthUser(scope.row)"
            v-hasPermi="['system:role:remove']"
          >取消任免</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-user ref="select" :postId="queryParams.postId" @ok="handleQuery" />
  </div>
</template>

<script lang="ts">
import { allocatedUserList2, authUserCancel2, authUserCancelAll2 } from "@/api/system/role";
import selectUser from "./selectUser2.vue";
import dictTag from "./DictTag/index.vue";
import {createDict} from "@/common/dict.ts";
export default {
  name: "AuthUser",
  dicts: ['sys_normal_disable', 'sys_user_sex','political_outlook','highest_educational','highest_major'],
  components: { selectUser,dictTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      dict:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postId: undefined,
        userName: undefined,
        phonenumber: undefined
      }
    };
  },
  async created() {
    this.dict =  await createDict(['sys_normal_disable', 'sys_user_sex','political_outlook','highest_educational','highest_major']) //模块字典值初始化
    console.log(this.dict)
    const postId = this.$route.params && this.$route.params.postId;
    if (postId) {
      this.queryParams.postId = postId;
      this.getList();
    }
    
  },
  methods: {
    getDict(key){
      console.log(key,this.dict.dictList[this.dict.dictIndex.indexOf(key)])
      return this.dict.dictList[this.dict.dictIndex.indexOf(key)]
    },
    /** 查询任免用户列表 */
    getList() {
      this.loading = true;
      allocatedUserList2(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose() {
      this.$router.go(-1)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId)
      this.multiple = !selection.length
    },
    /** 打开任免用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show();
    },
    /** 取消任免按钮操作 */
    cancelAuthUser(row) {
      const postId = this.queryParams.postId;
      this.$confirm('确认要取消该用户"' + row.userName + '"任免吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
        return authUserCancel2({ userId: row.userId, postId: postId });
      }).then(() => {
        this.getList();
        this.$message.success("取消任免成功");
      }).catch(() => {});
    },
    /** 批量取消任免按钮操作 */
    cancelAuthUserAll(row) {
      const postId = this.queryParams.postId;
      const userIds = this.userIds.join(",");
      this.$confirm('是否取消选中用户任免数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
        return authUserCancelAll2({ postId: postId, userIds: userIds });
      }).then(() => {
        this.getList();
        this.$message.success("取消任免成功");
      }).catch(() => {});
    }
  }
};
</script>